package es.ibm.santillana.mdm.autores;


import com.ibm.pim.catalog.item.Item;
import com.ibm.pim.common.exceptions.PIMInvalidOperationException;
import com.ibm.pim.context.PIMContextFactory;
import com.ibm.pim.hierarchy.Hierarchy;
import com.ibm.pim.hierarchy.HierarchyManager;
import com.ibm.pim.hierarchy.category.Category;

public class Autor {
	
	 

	public Autor( ) {
			
			
	}
	
	/**
	 * Mapped an autor in hierarchy abc. Uses the first character of the apellido1 to select the category
	 * @param autor
	 * @throws PIMInvalidOperationException
	 */
	public static void mappedAutorInABC (Item autor) throws PIMInvalidOperationException {
		HierarchyManager hiMng = PIMContextFactory.getCurrentContext().getHierarchyManager();
		Hierarchy hiABC = hiMng.getHierarchy("ABC");
		String apellido1 = (String)autor.getAttributeValue("Autores CPS/Apellido1");
		String letter = apellido1.substring(0, 1);
		Category catABC = hiABC.getCategoryByPrimaryKey(letter);
		autor.mapToCategory(catABC);
	}
	/**
	 * displaname creates an author from the first name and surname
	 * @param autor
	 */
	public static void createDisplayName (Item autor) {
		
		String nombre = (String)autor.getAttributeValue("Autores CPS/Nombre");
		String apellido1 = (String)autor.getAttributeValue("Autores CPS/Apellido1");
		autor.setAttributeValue("Autores CPS/DisplayName", apellido1 + ", " + nombre);
	}

}
